package org.csu.mypetstore_seller.exception;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;


@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = NumberFormatException.class)
    public ModelAndView appendException(NumberFormatException e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("catalog/afterOperate.html");
        return modelAndView;
    }
    @ExceptionHandler(IllegalArgumentException.class)
    public void handleIllegalArgumentException(IllegalArgumentException e, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("<script>alert('" + e.getMessage() + "'); window.history.back();</script>");
    }

}
